
cmake_minimum_required(VERSION 3.8)
project(rms_pkg)

if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rosidl_typesupport_cpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(spdlog REQUIRED)
find_package(fmt REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core Network Sql)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# 生成消息接口
rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/Heartbeat.msg"
  "msg/SensorData.msg"
  "srv/RobotStatus.srv"
  "action/TaskExecution.action"
  "action/MapDownload.action"
  DEPENDENCIES std_msgs
  DEPENDENCIES geometry_msgs
)

# (1) 心跳发布者节点
add_executable(heartbeat_publisher 
  src/heartbeat_publisher.cpp
  include/log_manager.cpp
)
ament_target_dependencies(heartbeat_publisher
  rclcpp
  std_msgs
  rosidl_default_runtime
)

# 链接消息接口
target_include_directories(heartbeat_publisher PRIVATE
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
)
target_link_libraries(heartbeat_publisher
  ${PROJECT_NAME}__rosidl_typesupport_cpp  # 直接链接项目自身的类型支持库
  spdlog::spdlog
  fmt::fmt
)
# 链接类型支持库
rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(heartbeat_publisher ${typesupport_target})

install(TARGETS heartbeat_publisher DESTINATION lib/${PROJECT_NAME})

# (2) 传感器HCD5014发布者节点
add_executable(sensor_hcd5014_publisher 
  src/sensor_hcd5014_publisher.cpp
  include/modbus_rtu.cpp
  include/log_manager.cpp
  include/db/database_manager.cpp
)
# 链接消息接口
target_include_directories(sensor_hcd5014_publisher PRIVATE
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
)
target_link_libraries(sensor_hcd5014_publisher
  ${PROJECT_NAME}__rosidl_typesupport_cpp
  Qt5::Core
  Qt5::Sql
  spdlog::spdlog
  fmt::fmt
)
# 链接类型支持库
rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(sensor_hcd5014_publisher ${typesupport_target})
ament_target_dependencies(sensor_hcd5014_publisher 
  rclcpp 
  std_msgs
)
install(TARGETS sensor_hcd5014_publisher DESTINATION lib/${PROJECT_NAME})

# (3) 传感器HCD5014订阅者节点
#add_executable(sensor_hcd5014_subscriber 
#  src/sensor_hcd5014_subscriber.cpp
#)
# 链接消息接口
#target_include_directories(sensor_hcd5014_subscriber PRIVATE
#  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
#  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
#)
#target_link_libraries(sensor_hcd5014_subscriber
#  ${PROJECT_NAME}__rosidl_typesupport_cpp  # 直接链接项目自身的类型支持库
#)
# 链接类型支持库
#rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
#target_link_libraries(sensor_hcd5014_subscriber ${typesupport_target})

#ament_target_dependencies(sensor_hcd5014_subscriber 
#  rclcpp 
#  std_msgs
#)
#install(TARGETS sensor_hcd5014_subscriber DESTINATION lib/${PROJECT_NAME})

# (4) 传感器m01h20发布者节点
add_executable(sensor_m01h20_publisher 
  src/sensor_m01h20_publisher.cpp
  include/modbus_rtu.cpp
  include/log_manager.cpp
  include/db/database_manager.cpp
)
# 链接消息接口
target_include_directories(sensor_m01h20_publisher PRIVATE
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
)
target_link_libraries(sensor_m01h20_publisher
  ${PROJECT_NAME}__rosidl_typesupport_cpp
  Qt5::Core
  Qt5::Sql
  spdlog::spdlog
  fmt::fmt
)
# 链接类型支持库
rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(sensor_m01h20_publisher ${typesupport_target})
ament_target_dependencies(sensor_m01h20_publisher 
  rclcpp 
  std_msgs
)
install(TARGETS sensor_m01h20_publisher DESTINATION lib/${PROJECT_NAME})

# 安装 Launch/config 文件
install(DIRECTORY
  launch
  config
  DESTINATION share/${PROJECT_NAME}
)

# (5) 任务服务Action节点
add_executable(task_action_server
  src/task_action_server.cpp
  include/seer/SCStatusTcp.cpp
  include/seer/RequestManager.cpp
  include/db/database_manager.cpp
  include/seer/StatusWorker.cpp
  include/log_manager.cpp
)

ament_target_dependencies(task_action_server
  rclcpp
  rclcpp_action
)

rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(task_action_server
  ${typesupport_target}
  Qt5::Core
  Qt5::Sql
  Qt5::Network
  fmt::fmt
  spdlog::spdlog
)

install(TARGETS task_action_server DESTINATION lib/${PROJECT_NAME})

# (6) Robot状态查询服务节点
add_executable(robot_status_server
  src/robot_status_server.cpp
  include/seer/SCStatusTcp.cpp
  include/seer/RequestManager.cpp
  include/seer/StatusWorker.cpp
  include/log_manager.cpp  
)

ament_target_dependencies(robot_status_server
  rclcpp
  rclcpp_action
)

rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(robot_status_server
  ${typesupport_target}
  Qt5::Core
  Qt5::Network
  fmt::fmt
  spdlog::spdlog
)

install(TARGETS robot_status_server DESTINATION lib/${PROJECT_NAME})


# (7) Robot地图下载Action服务节点
#add_executable(map_download_action_server
#  src/map_download_action_server.cpp
#  include/seer/SCStatusTcp.cpp
#  include/seer/RequestManager.cpp
#  include/seer/StatusWorker.cpp
#  include/log_manager.cpp  
#)

#ament_target_dependencies(map_download_action_server
#  rclcpp
#  rclcpp_action
#)

#rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
#target_link_libraries(map_download_action_server
#  ${typesupport_target}
#  Qt5::Core
#  Qt5::Network
#  fmt::fmt
#  spdlog::spdlog
#)

#install(TARGETS map_download_action_server DESTINATION lib/${PROJECT_NAME})

# (8) Robot状态查询服务节点
add_executable(robot_tcp_server
  src/robot_tcp_server.cpp
  include/seer/SCHeadData.h
  include/log_manager.cpp  
)

ament_target_dependencies(robot_tcp_server
  rclcpp
  rclcpp_action
)

rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(robot_tcp_server
  ${typesupport_target}
  Qt5::Core
  Qt5::Network
  fmt::fmt
  spdlog::spdlog
)

install(TARGETS robot_tcp_server DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_export_dependencies(rosidl_default_runtime)
ament_package()
